package com.engine.modules.vector.controller;

import com.engine.common.baseEntity.jsonResponse.JsonResponseEntity;
import com.engine.common.constant.CommonConstant;
import com.engine.common.util.TokenVerifyUtil.AuthRedisUtil;
import com.engine.common.util.TokenVerifyUtil.TokenVerifyUtil;
import com.engine.modules.vector.mapper.VectorTilesMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@RestController
@Api(tags = "1-矢量切片接口")
@ApiSort(value = 1)
@CrossOrigin
@RequestMapping("/Vector/Tiles")
@Slf4j
public class VectorTilesController {

    @Autowired
    AuthRedisUtil authRedisUtil;
    @Autowired
    VectorTilesMapper vectorTilesMapper;

    @ApiOperation(value = "z,x,y获取矢量切片", notes = "通过切片号xyz，获取单个的矢量切片，切片中的图层名是切片的表名")
    @GetMapping(value = "/selectVectorTile/{tableName}/{z}/{x}/{y}/{token}")
    @ApiOperationSupport(order = 1)

    public byte[] selectVectorTile(@PathVariable("tableName") String tableName,
                                   @PathVariable("z") int z, @PathVariable("x") int x, @PathVariable("y") int y,
                                   @PathVariable("token") String token,
                                   HttpServletResponse response) {
        try {
            String access = TokenVerifyUtil.tokenVerify(token);
            byte[] b = vectorTilesMapper.selectTileByXYZ(z, x, y, tableName, tableName, access);
            return b;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "查询出错，e：" + e.getMessage(), response);
            return jsonResponseEntity.getJsonString().getBytes();
        }
    }

    @ApiOperation(value = "z,x,y获取矢量切片组", notes = "通过切片号xyz，多个矢量切片组成的组，组在sys_table_to_group表中和多张表关联，组内的每个表都是一个图层，图层名在sys_table_to_group定义")
    @GetMapping(value = "/selectGroupVectorTile/{groupName}/{z}/{x}/{y}/{token}")
    @ApiOperationSupport(order = 2)
    public byte[] selectGroupVectorTile(@PathVariable("groupName") String groupName,
                                        @PathVariable("z") int z, @PathVariable("x") int x, @PathVariable("y") int y,
                                        @PathVariable("token") String token,
                                        HttpServletResponse response) {
        try {
            String access = TokenVerifyUtil.tokenVerify(token);
            byte[] b = vectorTilesMapper.selectGroupTileByXYZ(z, x, y, groupName, access);
            return b;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "查询出错，e：" + e.getMessage(), response);
            return jsonResponseEntity.getJsonString().getBytes();
        }
    }
}
